Skip to content

fix(Router): invalidate compile cache when types change#2

Merged
Jakiboy merged 1 commit into
masterfrom
copilot/fix-cache-staleness-on-addtypes
Apr 21, 2026
Merged

fix(Router): invalidate compile cache when types change#2
Jakiboy merged 1 commit into
masterfrom
copilot/fix-cache-staleness-on-addtypes

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 21, 2026

compile() memoizes compiled regex by route string alone, but the output also depends on $this->types. Calling addTypes() after routes have already been compiled left stale cache entries, causing incorrect pattern matching for any type modified after first use.

Change

  • addTypes() now resets $this->cache = [] after merging, ensuring subsequent match() calls recompile routes against the updated type map.
public function addTypes(array $types) : void
{
    $this->types = Arrayify::merge($this->types, $types);
    $this->cache = []; // invalidate stale compiled regex
}

@Jakiboy Jakiboy marked this pull request as ready for review April 21, 2026 22:43
Copilot AI review requested due to automatic review settings April 21, 2026 22:43
@Jakiboy Jakiboy merged commit 18e2f3d into master Apr 21, 2026
3 checks passed
@Jakiboy Jakiboy deleted the copilot/fix-cache-staleness-on-addtypes branch April 21, 2026 22:45
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a router correctness bug where compile() memoizes compiled route regex by route string only, even though compilation depends on the router’s type map ($this->types). After addTypes() updates types, previously compiled regex patterns could remain stale and cause incorrect matching.

Changes:

  • Invalidate the compiled-regex cache when addTypes() mutates the route type map.
  • Minor formatting alignment in addTypes() assignments.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Http/Router.php
Comment on lines 104 to 108
public function addTypes(array $types) : void
{
$this->types = Arrayify::merge($this->types, $types);
$this->types = Arrayify::merge($this->types, $types);
$this->cache = [];
}
Copy link

Copilot AI Apr 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change fixes a cache invalidation bug, but there’s no regression test ensuring that calling addTypes() after an initial match() (which populates the compile() cache) causes subsequent match() calls to use the updated type regex. Please add a test that primes the cache with one type definition, then overrides that type via addTypes(), and asserts that matching behavior changes accordingly.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants